package com.example.androidclassten;

import android.app.IntentService;
import android.content.Intent;
import android.content.Context;
import android.util.Log;

/**
 * An {@link IntentService} subclass for handling asynchronous task requests in
 * a service on a separate handler thread.
 * <p>
 * <p>
 * TODO: Customize class - update intent actions, extra parameters and static
 * helper methods.
 */
public class MyIntentService extends IntentService {

    private static final String ACTION_FOO = "com.example.androidclassten.action.FOO";
    private static final String ACTION_BAZ = "com.example.androidclassten.action.BAZ";

    private static final String EXTRA_PARAM1 = "com.example.androidclassten.extra.PARAM1";
    private static final String EXTRA_PARAM2 = "com.example.androidclassten.extra.PARAM2";

    public MyIntentService() {
        super("MyIntentService");
    }

//     启动任务
    public static void startActionFooA(Context context, String param1, String param2) {
//        启动服务任务
        Intent intent = new Intent(context, MyIntentService.class);
        intent.setAction(ACTION_FOO);
        intent.putExtra(EXTRA_PARAM1, param1);
        intent.putExtra(EXTRA_PARAM2, param2);
        context.startService(intent);
    }
    public static void startActionFooB(Context context, String param1, String param2) {
        Intent intent = new Intent(context, MyIntentService.class);
        intent.setAction(ACTION_FOO);
        intent.putExtra(EXTRA_PARAM1, param1);
        intent.putExtra(EXTRA_PARAM2, param2);
        context.startService(intent);
    }

    public static void startActionBaz(Context context, String param1, String param2) {
        Intent intent = new Intent(context, MyIntentService.class);
        intent.setAction(ACTION_BAZ);
        intent.putExtra(EXTRA_PARAM1, param1);
        intent.putExtra(EXTRA_PARAM2, param2);
        context.startService(intent);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            final String action = intent.getAction();
            if (ACTION_FOO.equals(action)) {
                final String param1 = intent.getStringExtra(EXTRA_PARAM1);
                final String param2 = intent.getStringExtra(EXTRA_PARAM2);
                handleActionFoo(param1, param2);
            } else if (ACTION_BAZ.equals(action)) {
                final String param1 = intent.getStringExtra(EXTRA_PARAM1);
                final String param2 = intent.getStringExtra(EXTRA_PARAM2);
                handleActionBaz(param1, param2);
            }
        }
    }

    private static final String TAG = "handleActionFoo";
//    启动后执行的代码
    private void handleActionFoo(String param1, String param2) {
        Log.d(TAG, "handleActionFoo: start, param1=" + param1 + ",param2="+ param2);
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Log.d(TAG, "handleActionFoo: stop");
    }

    private void handleActionBaz(String param1, String param2) {
        Log.d(TAG, "handleActionFoo: start, param1=" + param1 + ",param2="+ param2);
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Log.d(TAG, "handleActionFoo: stop");
    }
}